﻿using System.Web.Mvc;
using ENail.Domain.Core;
using ENail.Domain.Core.Contract.Services;
using ENail.Portal.Web.Models;
using Equation.Enterprise.Utilities.DesignByContract;

namespace ENail.Portal.Web.Controllers
{
    public class AssignmentController : BaseController
    {
        private readonly IEmployeeService _employeeService;
        private readonly INailServiceService _nailServiceService;
        private readonly IRegistrationService _registrationService;

        public AssignmentController(IEmployeeService employeeService, INailServiceService nailServiceService, IRegistrationService registrationService)
        {
            Check.Require(employeeService != null);
            Check.Require(nailServiceService != null);
            Check.Require(registrationService != null);

            _employeeService = employeeService;
            _nailServiceService = nailServiceService;
            _registrationService = registrationService;
        }

        public ActionResult Index(int? id)
        {
            if (!id.HasValue)
                return RedirectToAction("Add", "Registration", null);

            var model = PrepareViewModel(id.Value);
            return View(model);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Update(int id, AssignmentViewModel viewModel)
        {
            if (id == 0 || !ValidateAssignment(viewModel))
            {
                var model = PrepareViewModel(id);
                return View("Index", model);
            }

            var registration = _registrationService.GetRegistration(id);
            if (registration != null)
                registration.AddAssignment(new Assignment
                                               {
                                                   Employee = _employeeService.GetEmployee(viewModel.EmployeeId),
                                                   NailService = _nailServiceService.GetNailService(viewModel.NailServiceId)
                                               });
            _registrationService.UpdateRegistration(registration);

            return RedirectToAction("Index", new {@id = id});
        }

        private bool ValidateAssignment(AssignmentViewModel model)
        {
            if (model.EmployeeId == 0)
            {
                ModelState.AddModelError("Employee", "You must choose an employee");
            }
            if (model.NailServiceId == 0)
            {
                ModelState.AddModelError("NailService", "You must choose a nail service");
            }

            return ModelState.IsValid;
        }

        private AssignmentViewModel PrepareViewModel(int id)
        {
            var model = new AssignmentViewModel();
            var mapper = new AssignmentViewModelMapper(_employeeService, _nailServiceService);
            //var registration = _registrationService.GetRegistration(id);
            //mapper.MapDomainToView(registration, model);
            return model;
        }
    }
}